package com.yanqu.road.server.cmd.activity.common;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.common.CommonActivityModule;
import com.yanqu.road.server.pb.CommonActivityPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = GameProtocol.S_COMMON_ACTIVITY_SET_AUTO_BUY_PARAM,desc = "万能活动设置自动购买")
public class CommonActivitySetAutoBuyCmd extends AbstractCommand {

    private int activityId;
    private int childType;
    private Map<String,Integer> map;


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CommonActivityProto.SetCommonActivityAutoBuyDataReq req =CommonActivityProto.SetCommonActivityAutoBuyDataReq.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        childType = req.getData().getChildType();
        map = new HashMap<>();
        for (CommonActivityProto.CommonActivityAutoBuyData data:req.getData().getDataList()){
            map.put(data.getItem(),data.getNum());
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = player.getModule(CommonActivityModule.class).setCommonActivityAutoBuyData(activityId,childType,map);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0){
            //回包
            CommonActivityProto.SetCommonActivityAutoBuyDataResp.Builder builder = CommonActivityProto.SetCommonActivityAutoBuyDataResp.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_COMMON_ACTIVITY_SET_AUTO_BUY_PARAM, builder);
        }
    }
}
